package com.example.sprinbootdemo.controller;

import com.example.sprinbootdemo.entity.ApiResponse;
import com.example.sprinbootdemo.entity.RentManagement;
import com.example.sprinbootdemo.service.RentManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/rentmanagements")
public class RentManagementController {

    private static final Logger logger = LoggerFactory.getLogger(RentManagementController.class);
    @Autowired
    private RentManagementService rentManagementService;

    // 根据 RentID 获取租赁记录
    @GetMapping("/get/{rentID}")
    public ResponseEntity<ApiResponse> getRentManagementByRentID(@PathVariable int rentID) {
        try {
            RentManagement rentManagement = rentManagementService.getRentManagementByRentID(rentID);
            if (rentManagement == null) {
                ApiResponse response = new ApiResponse(0, "数据不存在", null);
                return ResponseEntity.status(404).body(response);
            }
            ApiResponse response = new ApiResponse(1, "成功", rentManagement);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取数据失败,RentID: {}", rentID, e);
            ApiResponse response = new ApiResponse(0, "获取数据失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 获取所有的租赁记录
    @GetMapping("/getList")
    public ResponseEntity<ApiResponse> getList() {
        List<RentManagement> list;
        try {
            list = rentManagementService.getAllRentManagements();
            ApiResponse response = new ApiResponse(1, "成功", list);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("获取租赁记录列表失败", e);
            ApiResponse response = new ApiResponse(0, "获取租赁记录列表失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 添加新的租赁记录
    @PostMapping("/addDate")
    public ResponseEntity<ApiResponse> addDate(@RequestBody RentManagement rentManagement) {
        try {
            logger.info("添加租赁记录: {}", rentManagement);
            rentManagementService.addRentManagement(rentManagement);
            ApiResponse response = new ApiResponse(1, "租赁记录添加成功", null);
            return ResponseEntity.status(201).body(response); // 返回 201 表示创建成功
        } catch (Exception e) {
            logger.error("添加租赁记录失败", e);
            ApiResponse response = new ApiResponse(0, "添加租赁记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 删除指定 ID 的租赁记录
    @DeleteMapping("/delete/{rentID}")
    public ResponseEntity<ApiResponse> deleteRentManagementByRentID(@PathVariable int rentID) {
        try {
            logger.info("删除租赁记录, RentID: {}", rentID);
            rentManagementService.deleteRentManagementByRentID(rentID);
            ApiResponse response = new ApiResponse(1, "租赁记录删除成功", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("删除租赁记录失败, RentID: {}", rentID, e);
            ApiResponse response = new ApiResponse(0, "删除租赁记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }

    // 更新指定 ID 的租赁记录
    @PutMapping("/update")
    public ResponseEntity<ApiResponse> updateRentManagementByRentID(@RequestBody RentManagement rentManagement) {
        try {
            logger.info("更新租赁记录: {}", rentManagement);
            rentManagementService.updateRentManagementByRentID(rentManagement);
            ApiResponse response = new ApiResponse(1, "租赁记录更新成功", null);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("更新租赁记录失败", e);
            ApiResponse response = new ApiResponse(0, "更新租赁记录失败: " + e.getMessage(), null);
            return ResponseEntity.status(500).body(response);
        }
    }
}